package com.situ.controller.might;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.situ.service.might.SysMightService;
import com.situ.entity.might.SysMight;
import com.situ.util.*;

import java.util.List;

/**
 * SysMight的路由接口服务
 * 
 * @author胡文
 *
 */
//
@RestController
public class SysMightController {

	/** SysMightService服务 */
	@Autowired
	private SysMightService sysMightService;
	
	/**
	 * 查询所有SysMight数据的方法
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "/SysMight/query", produces = {"application/json;charset=UTF-8"})
	public ResponseData query(SysMight value) {

		return ResponseData.ok().putDataValue("list",sysMightService.find(value));
	}
	
	/**
	 * 通过id查询SysMight数据的方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SysMight/queryById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData queryById(@PathVariable(name="id") int id) {
		 return ResponseData.ok().putDataValue("obj",sysMightService.findOne(id));
	}
	
	/**
	 * 插入SysMight属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SysMight/save")
	public ResponseData save(SysMight value) {
		return ResponseData.ok().putDataValue("obj", sysMightService.saveNotNull(value));
	}
	
	/**
	 * 更新SysMight属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SysMight/update", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(SysMight value) {
		return ResponseData.ok().putDataValue("obj",sysMightService.updateNotNullById(value));
	}

	/**
	 * 通过id删除SysMight数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SysMight/delById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delById(@PathVariable(name="id") int id) {
		return ResponseData.ok().putDataValue("obj",sysMightService.deleteById(id));
	}


    //带分页的查询全部
//@ApiOperation("带分页的查询全部")
    @RequestMapping(value = "/SysMight/queryPage", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryPage(SysMight value,
                             @RequestParam(defaultValue = "1",required = false) int pageNum ,
                             @RequestParam(defaultValue = "5",required = false) int pageSize) {

	    PageHelper.startPage(pageNum, pageSize);
        List<SysMight> list = sysMightService.queryRights(value);
        PageInfo pageInfo = new PageInfo<SysMight>(list,5);

        return ResponseData.ok()
        // .putDataValue("list", list)
        .putDataValue("pageInfo",pageInfo);
        }

    @RequestMapping(value = "getMenuByRoleId/{roleId}", produces = {"application/json;charset=UTF-8"})
    public ResponseData getMenuByRoleId(@PathVariable Integer roleId){
        return ResponseData.ok().putDataValue("mights", sysMightService.queryMightByRoleId(roleId));
    }
    @RequestMapping(value = "/getMenuByRoleId2/{roleId}", produces = {"application/json;charset=UTF-8"})
    public ResponseData getMenuByRoleId2(@PathVariable Long roleId){
        return ResponseData.ok().putDataValue("mights",sysMightService.getMenuByRoleId2(roleId));
    }
    @PostMapping(value = "/saveMightToRose", produces = {"application/json;charset=UTF-8"})
    public ResponseData saveMightToRose( Integer []arr,int roleid ) {
        for( int i : arr){
             sysMightService.saveMightToRose(i,roleid);
        }
        return ResponseData.ok();
    }
    @GetMapping(value = "/delMightToRose/{roleId}", produces = {"application/json;charset=UTF-8"})
    public ResponseData delMightToRose(@PathVariable Long roleId){
        return ResponseData.ok().putDataValue("i", sysMightService.delMightToRose(roleId));
    }
    @RequestMapping(value = "/SysMight/saveMight", produces = {"application/json;charset=UTF-8"})
    public ResponseData saveMight(SysMight sysMight){
        return ResponseData.ok().putDataValue("i", sysMightService.saveMight(sysMight));
    }
    /**
     * 通过id删除SysMight数据方法
     *
     * @return
     */
    @RequestMapping(value = "/SysMight/del/{id}", produces = {"application/json;charset=UTF-8"})
    public ResponseData del(@PathVariable(name="id") int id) {
        try {
            return ResponseData.ok().putDataValue("obj",sysMightService.deleteById(id));
        }catch (Exception e){
            return ResponseData.notFound();
        }
    }

}